package com.huluobo.a2109mvpimdemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.huluobo.a2109mvpimdemo.R;
import com.huluobo.a2109mvpimdemo.chat.ChatActivity;
import com.huluobo.a2109mvpimdemo.chat.MyChatActivity;
import com.huluobo.a2109mvpimdemo.contact.ContactListItemBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LC on 2023/11/20.
 */
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {
    private List<ContactListItemBean> listItemBeans;
    private Context context;

    public ContactAdapter(List<ContactListItemBean> listItemBeans, Context context) {
        this.listItemBeans = listItemBeans;
        this.context = context;
    }

    //绑定布局
    @NonNull
    @Override
    public ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ContactViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact, parent,
                false));
    }

    //绑定数据
    @Override
    public void onBindViewHolder(@NonNull ContactViewHolder holder, int position) {
        holder.username.setText(listItemBeans.get(position).getUsername());
        if (listItemBeans.get(position).isShowFirstLetter()) {
            holder.firstLetter.setVisibility(View.VISIBLE);
            holder.firstLetter.setText(listItemBeans.get(position).getFirstLetter() + "");
        } else {
            holder.firstLetter.setVisibility(View.GONE);
        }

        holder.linearLayout.setOnClickListener(v -> {
            String conversationId = listItemBeans.get(position).getUsername();
            Intent intent = new Intent(context, MyChatActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("conversationId", conversationId);
            bundle.putInt("chatType", 1);
            intent.putExtras(bundle);
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return listItemBeans.size();
    }

    class ContactViewHolder extends RecyclerView.ViewHolder {
        TextView username, firstLetter;
        LinearLayout linearLayout;

        public ContactViewHolder(@NonNull View itemView) {
            super(itemView);
            username = itemView.findViewById(R.id.item_contact_username);
            firstLetter = itemView.findViewById(R.id.item_contact_firstLetter);
            linearLayout = itemView.findViewById(R.id.item_contact_ll);
        }
    }
}
